package com.ant.cn.duomen01;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

import cn.dow.android.DOW;
import cn.dow.android.listener.DataListener;
import cn.dow.android.listener.DownloadListener;


public class AppDetailActivity extends Activity {
	private final String TAG = AppDetailActivity.class.getSimpleName();
	private int id;
	private Map<String, Object> mData;
	private LinearLayout ll_back;
	private TextView tv_title;
	private TextView tv_appName;
	private TextView tv_appSize;
	private TextView tv_appVersion;
	private TextView tv_taskPointInfo;
	private TextView tv_taskInfo1;
	private TextView tv_otherInfo;
	private Button btn_download;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		id = getIntent().getIntExtra("id", -1);
		setContentView(R.layout.dm_layout_offer_detail);
		initPane();
		getData();
	}

	/**
	 * 初始化View
	 */
	private void initPane() {
		ll_back = (LinearLayout) findViewById(R.id.more_activity_title_ll_back);
		ll_back.setOnClickListener(backClick);
		tv_title = (TextView) findViewById(R.id.more_activity_title_tv_content);
		tv_appName = (TextView) findViewById(R.id.dm_layout_offer_detail_tv_name);
		tv_appSize = (TextView) findViewById(R.id.dm_layout_offer_detail_size);
		tv_appVersion = (TextView) findViewById(R.id.dm_layout_offer_detail_version);
		tv_taskPointInfo = (TextView) findViewById(R.id.dm_layout_offer_detail_tv_task_pointInfo);
		tv_taskInfo1 = (TextView) findViewById(R.id.dm_layout_offer_detail_taskInfo);
		tv_otherInfo = (TextView) findViewById(R.id.dm_layout_offer_detail_tv_other_info);

		btn_download = (Button) findViewById(R.id.dm_layout_offer_detail_btn_download);
		btn_download.setOnClickListener(taskButtonClick);

	}

	private void getData() {
		DOW.getInstance(mContext).getAdDetail(this, id, new DataListener() {

			@Override
			public void onResponse(Object... arg0) {
				if (arg0 != null && arg0.length > 0) {
					mData = (Map<String, Object>) arg0[0];
					setData();
				}
			}

			@Override
			public void onError(String arg0) {
				Toast.makeText(getApplicationContext(), arg0,
						Toast.LENGTH_SHORT).show();
			}
		});
		DOW.getInstance(mContext).registerDownLoadListener(mDownloadListener);
	}

	/**
	 * 给View 填充数据
	 */
	private void setData() {
		if (null == mData) {
			return;
		}
		// 广告名称
		tv_title.setText(mData.get("name").toString());
		// 广告名称
		tv_appName.setText(mData.get("name").toString());
		// 广告大小
		tv_appSize.setText("大小：" + mData.get("size"));
		// 版本号
		tv_appVersion.setText("版本：" + mData.get("ver"));
		// 广告可获得积分
		tv_taskPointInfo.setText("共" + mData.get("point")
				+ DOW.getInstance(mContext).getUnitName());
		// 按钮名称
		String button_text = mData.get("button_text").toString();
		if (!TextUtils.isEmpty(button_text)) {
			btn_download.setText(button_text);
		}
		try {
			JSONArray ja = new JSONArray(mData.get("tasks").toString());
			StringBuffer task_desc = new StringBuffer();
			if (null != ja && ja.length() > 0) {
				for (int i = 0; i < ja.length(); i++) {
					JSONObject obj = ja.getJSONObject(i);
					String desc = obj.optString("desc");
					int point = obj.optInt("point");

					task_desc.append((i + 1) + "." + desc + point
							+ DOW.getInstance(mContext).getUnitName());
					task_desc.append("\n");
				}
			}
			tv_taskInfo1.setText(task_desc.toString());
		} catch (Exception e) {
		}

		String otheInfo = "LOGO地址==>> logo=" + mData.get("logo") + "\n\n"
				+ "是否可进入详情===>>open_detail=" + mData.get("open_detail")
				+ "\n\n" + "当前任务是否可做===>>executable=" + mData.get("executable")
				+ "\n\n" + "应用简介===>>desc=" + mData.get("description");
		tv_otherInfo.setText(otheInfo);
	}

	OnClickListener taskButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// 应用详情内点击下载
			download();
		}
	};

	private void download() {
		int id = (Integer) mData.get("id");
		// 应用详情内点击下载
		DOW.getInstance(mContext).download(AppDetailActivity.this, id,
				mData.get("pack_name").toString());
	}

	private DownloadListener mDownloadListener = new DownloadListener() {

		@Override
		public void onProgressChange(int id, long total, long process) {
			String str_progress = (process * 100 / total) + "%";
			btn_download.setText(str_progress);
			Log.v(TAG, "onProgressChange id:" + id + " Progress:"
					+ str_progress);
		}

		@Override
		public void onInstallSuccess(int id) {
			Log.v(TAG, "onInstallSuccess id:" + id);
		}

		@Override
		public void onDownloadSuccess(int id) {
			Log.v(TAG, "onDownloadSuccess id:" + id);
		}

		@Override
		public void onDownloadStart(int id) {
			Log.v(TAG, "onDownloadStart id:" + id);
		}

		@Override
		public void onDownloadFailed(int id, String error) {
			Log.v(TAG, "onDownloadFailed id:" + id);
		}
	};
	OnClickListener backClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			AppDetailActivity.this.finish();
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		DOW.getInstance(mContext)
				.unRegisterDownloadListener(mDownloadListener);
	}

}
